package com.eatme.web.common.vo;

import java.io.Serializable;

public class AppResponse<T> implements Serializable {

    private Boolean success;

    private Integer code;

    private String message;

    private T data;

    private Object exception;



    public Object getException() {
        return exception;
    }

    public void setException(Object exception) {
        this.exception = exception;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


    // 构造器私有
    private AppResponse(){

    }
    private AppResponse(Boolean success){

    }



    public static AppResponse success(){

        return new AppResponse(true);
    }

    // 通用返回成功
    public static <T> AppResponse ok() {
        AppResponse r = new AppResponse();
        r.setSuccess(ResponseCode.SUCCESS.getSuccess());
        r.setCode(ResponseCode.SUCCESS.getCode());
        r.setMessage(ResponseCode.SUCCESS.getMessage());
        return r;
    }

    public static <T> AppResponse ok(String message) {
        AppResponse r = new AppResponse();
        r.setSuccess(ResponseCode.SUCCESS.getSuccess());
        r.setCode(ResponseCode.SUCCESS.getCode());
        r.setMessage(message);
        return r;
    }


    public static <T> AppResponse ok(T data) {
        AppResponse r = new AppResponse();
        r.setSuccess(ResponseCode.SUCCESS.getSuccess());
        r.setCode(ResponseCode.SUCCESS.getCode());
        r.setMessage(ResponseCode.SUCCESS.getMessage());
        r.setData(data);
        return r;
    }

    public static AppResponse fail(String s) {
        AppResponse r = new AppResponse();
        r.setSuccess(ResponseCode.UNKNOWN_ERROR.getSuccess());
        r.setCode(ResponseCode.UNKNOWN_ERROR.getCode());
        r.setMessage(ResponseCode.UNKNOWN_ERROR.getMessage());
        return r;
    }

    // 通用返回失败，未知错误
    public static AppResponse error() {
        AppResponse r = new AppResponse();
        r.setSuccess(ResponseCode.UNKNOWN_ERROR.getSuccess());
        r.setCode(ResponseCode.UNKNOWN_ERROR.getCode());
        r.setMessage(ResponseCode.UNKNOWN_ERROR.getMessage());
        return r;
    }

    public static AppResponse error(String message) {
        AppResponse r = new AppResponse();
        r.setSuccess(ResponseCode.UNKNOWN_ERROR.getSuccess());
        r.setCode(ResponseCode.UNKNOWN_ERROR.getCode());
        r.setMessage(message);
        return r;
    }

    // 设置结果，形参为结果枚举
    public static AppResponse setResult(ResponseCode result) {
        AppResponse r = new AppResponse();
        r.setSuccess(result.getSuccess());
        r.setCode(result.getCode());
        r.setMessage(result.getMessage());
        return r;
    }

    /**------------使用链式编程，返回类本身-----------**/

    // 自定义返回数据
    public AppResponse data(T obj) {
        this.setData(obj);
        return this;
    }
    // 通用设置data

    // 自定义状态信息
    public AppResponse message(String message) {
        this.setMessage(message);
        return this;
    }

    // 自定义状态码
    public AppResponse code(Integer code) {
        this.setCode(code);
        return this;
    }

    // 自定义返回结果
    public AppResponse success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public AppResponse exception(Object exception) {
        this.setException(exception);
        return this;
    }
}
